package com.swz.dcrm.adpter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SectionIndexer;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.swz.dcrm.R;
import com.swz.dcrm.base.ViewHolder;
import com.swz.dcrm.model.PersonalInfo;
import com.swz.dcrm.util.Tool;
import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersAdapter;
import com.xh.baselibrary.callback.OnClickListener;
import com.xh.baselibrary.widget.CircleImageView;


import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AddressBookAdapter extends CustomAdapter<PersonalInfo> implements StickyRecyclerHeadersAdapter<AddressBookAdapter.HeaderViewHolder>, SectionIndexer {

    private int type;

    private ArrayList<Integer> mSectionPositions;
    private List<Integer> charsIndex;
    public AddressBookAdapter(Context context, List<PersonalInfo> datas,int type) {
        super(context, R.layout.item_address_book, datas);
        this.type=type;
    }

    @Override
    protected void convert(ViewHolder holder, PersonalInfo personalInfo, int position) {
        Glide.with(mContext)
                .load(personalInfo.getHeadPortraitUrl())
                .into((CircleImageView) holder.getView(R.id.iv_headPic));
        holder.setText(R.id.tv_name,personalInfo.getName());
        holder.setText(R.id.tv_post,personalInfo.getRole().getRoleGroup());
        if(type==0){
            holder.getView(R.id.iv_call).setVisibility(View.VISIBLE);
            holder.setOnClickListener(R.id.iv_call, v -> {
                if(onClickListener!=null){
                    onClickListener.onItemClick(personalInfo);
                }
            });
        }else if(type==1){
            holder.getView(R.id.iv_next).setVisibility(View.VISIBLE);
            holder.itemView.setOnClickListener(v -> {
                if(onClickListener!=null){
                    onClickListener.onItemClick(personalInfo);
                }
            });
        }

    }

    @Override
    public long getHeaderId(int position) {
        if(getDatas()!=null&&getDatas().size()>0){


            if(isContainChinese(mDatas.get(position).getName())){
                return Tool.ToPinyin(mDatas.get(position).getName()).toLowerCase().charAt(0);
            }else {
                return mDatas.get(position).getName().toLowerCase().charAt(0);
            }
        }
       return 0 ;
    }

    @Override
    public HeaderViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_letter,parent,false);
        return new HeaderViewHolder(view);
    }

    @Override
    public void onBindHeaderViewHolder(HeaderViewHolder headerViewHolder, int i) {
        headerViewHolder.letter.setText(Tool.ToPinyin(mDatas.get(i).getName()).substring(0,1).toUpperCase());
    }

    @Override
    public Object[] getSections() {
        List<String> sections = new ArrayList<>(26);
        charsIndex=new ArrayList<>(26);
        List<String> chars=new ArrayList<>(26);
        mSectionPositions = new ArrayList<>(26);

        for(int i=0;i<mDatas.size();i++){
           String s=Tool.ToPinyin(mDatas.get(i).getName());
            String section =s.substring(0,1).toUpperCase();
            if (!sections.contains(section)) {
                sections.add(section);
                mSectionPositions.add(i);
            }
        }

        for(int i=0;i<26;i++){
            char c= (char) (i+65);
            String s=c+"";
            chars.add(s);
            if(sections!=null&&sections.size()>0){
                int index=-1;
                for(int k=0;k<sections.size();k++){
                    if(sections.get(k).equals(s)){
                        index=0;
                        charsIndex.add(mSectionPositions.get(k));
                        break;
                    }
                }
                if(index==-1){
                    charsIndex.add(0);
                }
            }


        }

        return chars.toArray(new String[0]);
    }

    @Override
    public int getPositionForSection(int sectionIndex) {
        return charsIndex.get(sectionIndex);
    }

    @Override
    public int getSectionForPosition(int position) {
       return 0;
    }

    public static class HeaderViewHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.tv_letter)
        TextView letter;

        public HeaderViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
    /**
     * 判断字符串中是否包含中文
     * @param str
     * 待校验字符串
     * @return 是否为中文
     * @warn 不能校验是否为中文标点符号
     */
    public static boolean isContainChinese(String str) {
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
    }
}
